|
|
 |
|
|
|
|
Dim G As Integer
Dim B As Integer
' Find the unit surface normal. It is the
' same for all the light sources. UnitNormalVector Nx, Ny, Nz
For Each light In light_sources ' Find the unit vector pointing towards the light. Lx = light.X - Points(1).coord(1)
Ly = light.Y - Points(1).coord(2)
Lz = light.Z - Points(1).coord(3)
L_len = Sqr(Lx * Lx + Ly * Ly + Lz * Lz)
Lx = Lx / L_len
Ly = Ly / L_len
Lz = Lz / L_len
' See how intense to make the color. NdotL = Nx * Lx + Ny * Ly + Nz * Lz
' The light does not hit the top of the
' surface if NdotL <= 0. If NdotL > 0 Then
R = R + light.Ir * DiffuseKr * NdotL
G = G + light.Ig * DiffuseKg * NdotL
B = B + light.Ib * DiffuseKb * NdotL
End If
Next light
' Keep the color components <= 255. If R > 255 Then R = 255
If G > 255 Then G = 255
If B > 255 Then B = 255
' Return the color. SurfaceColor = RGB(R, G, B)
End Function |
|
|
|
|
|
|
|
|
Example program Light1, shown in Figures 16.2 and 16.3, uses this subroutine to display objects with diffuse reflection. Use the option buttons to select a scene. The objects in the scenes all have their own predefined diffuse reflection coefficients. |
|
|
|
|
|
|
|
|
Use the check boxes to determine which predefined light sources the program should use. The spheres in the two last scenes are white and can be particularly interesting with the nonwhite color sources. |
|
|
|
|
|
|
|
|
Note that the program's light sources are stationary. When you press the arrow keys, the viewing position changes, but the objects and light sources do not. That means the angles the light makes with the objects do not change, so each polygon's color remains the same. |
|
|
|
|
|